package org.osami.syncagent.server.tcpip;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osami.identification.NodeIdentification;
import org.osami.syncagent.server.SyncAgentServer;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator {

	private String nodeID;
	private SyncAgentServer tcpipServer;
	private ServiceRegistration reg;

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		// Get node ID of local node
		/**
		 * Get local node id from Identification service
		 */
		NodeIdentification idserv = null;
		ServiceReference refs = context.getServiceReference(NodeIdentification.class.getName());
		if (refs == null) {
			System.out.println("EventAdmin unavailable on SyncAgent start");
		} 
		else {
			idserv = (NodeIdentification)context.getService(refs);
		}
		if (idserv == null)
		{
			System.out.println("OSAmI Node ID service not found");
		}
		else
		{
			nodeID = idserv.getId();
		}
		
		// Register service as a  OSGi service
		Dictionary<String, String> props = new Hashtable<String,String>();
        
        props.put("description", "TCP/IP implementation of SyncAgentServer");
   
		tcpipServer = (SyncAgentServer) new SyncAgentServerTCPIP(idserv);
		
		reg = context.registerService(SyncAgentServer.class.getName(), tcpipServer, props);
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		tcpipServer.shutdown();
	}

}
